package com.example.swordjava;


public class Test24 {

    public static void main(String[] args) {

        int[] a = {2, 3, 1, 0, 2, 5, 3};
    }

    class ListNode {
        ListNode next = null;
        int val;

        public ListNode(int val) {
            this.val = val;
        }
    }

    /**
     * 反转链表
     */

    public ListNode ReverseList(ListNode head) {
        // write code here
        if (head == null || head.next == null) return head;
        ListNode pre = null;
        ListNode cur = head;
        ListNode next;
        while (cur != null) {
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
    public ListNode ReverseListRecursion(ListNode head) {
        // write code here
        if (head == null || head.next == null) return head;
        ListNode newHead = ReverseListRecursion(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }

}
